//	--------------------------------------------------------------------
//	Copyright(C) 2006,2007 Zhao Yukun. All rights reserved.
//	This file is a part of the Xeres system.
//	Xeres 2007
//	Contact : xeres.engine@gmail.com
//	$(reservedInfo)
//	$(reservedDeclaration)
//	--------------------------------------------------------------------
///	@file	<CRenderTargetset9.cpp>
///	@path	~/src/graphics/dx9/
///	@date	2009/01/24
///	@desc	.

#include "config/config.h"

#include "graphics/RenderContext.h"
#include "graphics/dx9/CRenderTargetSet9.h"

namespace xeres {
	
	IMPL_IOBJECT_CLASS( CRenderTargetSet9 );

	// CRenderTargetSet9
	CRenderTargetSet9::CRenderTargetSet9( int width , int height )
		: m_width( width ) , m_height( height )
	{
	}
	// ~CRenderTargetSet9
	CRenderTargetSet9::~CRenderTargetSet9( void )
	{

	}
	// GetSize
	Pair<int> CRenderTargetSet9::GetSize( void )
	{
		return Pair<int>( m_width , m_height );
	}
	// GetCount
	uint CRenderTargetSet9::GetCount( void )
	{
		return 8;
	}
	// BindTarget
	void CRenderTargetSet9::BindTarget( uint index , RefWeak<IRenderTarget> target )
	{
		XS_ASSERT( index < 8 );
		m_slots[index] = target;
	}
	// UnbindAll
	void CRenderTargetSet9::UnbindAll( void )
	{
		for( int i = 0 ; i < 8 ; ++i )
			m_slots[i] = NULL;
	}
	// ApplyTarget
	void CRenderTargetSet9::ApplyTarget( RenderContext& rc )
	{
		IDirect3DDevice9 * d9 = rc.GetDX9();
		for( uint i = 0 ; i < 8 ; ++i )
		{
			IDirect3DSurface9 * rt = NULL;
			if( m_slots[i].IsValid() )
			{
				IUnknown * res = m_slots[i]->GetRenderTargetInterface();
				if( res )
				{
					res->QueryInterface( IID_IDirect3DSurface9 , (void**)&rt );
				}
			}
			if( rt )
			{
				d9->SetRenderTarget( i , rt );
				rt->Release();
			}
		}
	}
	
} // namespace xeres
